package net.boar.os;
import java.io.*;
import java.util.Properties;

/**
 * @Author: zgl
 * @Date: 2021/6/23 11:03
 * @Description:
 */
public class OperatingSystemDay03 {
    private static class Configuration {
        private String fileName;
        private String workerPath;
        private String osName;
        private String build;
    }

    private FloppyDiskEntity floppyDisk = new FloppyDiskEntity();
    private static Configuration cfg;
    private void writeToFloppy(String fineName) {
        File file = new File(fineName);
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            byte[] buf = new byte[512];
            buf[510] = 0x55;
            buf[511] = (byte) 0xaa;
            if (in.read(buf) != -1) {
                //0面,0柱,2,区
                floppyDisk.writeFloppy(0, 0, 1, buf);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public OperatingSystemDay03(String s) {
        writeToFloppy(s);
    }

    public void makeFloppy() {
        floppyDisk.makeFloppy(getConfiguration().workerPath + getConfiguration().osName + ".img");
    }

    public static void main(String[] args) {
        //nasm -o boot.bat boot.asm
        buildWork();
        OperatingSystemDay03 op = new OperatingSystemDay03(getConfiguration().workerPath + getConfiguration().fileName + ".bat");
        op.makeFloppy();
    }

    private static void buildWork() {
        WorkerEntity entity = new WorkerEntity();
        entity.setCmd("nasm -o " + getConfiguration().fileName +".bat "+ getConfiguration().fileName +".asm");
        entity.setWorkerPath(getConfiguration().workerPath);
        entity.setCreateBarName(getConfiguration().build + ".bat");
        entity.setEncoding("GBK");
        Build.builder(entity);
    }

    private static Configuration getConfiguration() {
        if(cfg != null){
            return cfg;
        }
        Configuration configuration = new Configuration();
        String property = System.getProperty("user.dir");
        String propertiesName = property + File.separator +"config" + File.separator + "path.properties";
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(propertiesName))){
            Properties properties = new Properties();
            properties.load(bufferedReader);
            configuration.fileName = properties.getProperty("fileName");
            String workerPath = properties.getProperty("workerPath");
            workerPath = workerPath.replace("/",File.separator);
            configuration.workerPath = workerPath;
            configuration.osName = properties.getProperty("osName");
            configuration.build = properties.getProperty("build");
            cfg = configuration;
        }catch (Exception e){
            e.printStackTrace();
        }
        return configuration;
    }
}
